<template>
    
    <!-- 组件的模板 -->
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">企业全称</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input too_much" maxlength="50" type="text" placeholder="请按照营业执照名称填写" v-model='obj.teamdetailname' :value="obj.teamdetailname"/>
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">企业简称</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" maxlength="20" type="text" placeholder="如：斗米" v-model='obj.teamname' :value="obj.teamname"/>
            </div>
        </div> 
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">联系人</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" maxlength="10" type="text" placeholder="请输入" v-model='obj.linkmanname' :value="obj.linkmanname"/>
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">手机号码</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" maxlength="20" type="tel" placeholder="请输入" v-model='obj.linkmannumber' :value="obj.linkmannumber"/>
            </div>
        </div>
    </div>
    <div class="weui-cells weui-cells_form">
        <a class="weui-cell weui-cell_access" href="javascript:;" v-on:click="industrySelect(obj.industry)">
            <div class="weui-cell__bd">
                <p v-if="obj.industry == '请选择'"><span class="title">行业</span><span class="placeholder">{{obj.industry}}</span></p>
                <p v-else><span class="title">行业</span>{{obj.industry}}</p>
            </div>
            <div class="weui-cell__ft"></div>
        </a>
        <a class="weui-cell weui-cell_access" href="dmsaas://SelectProvince">
            <div class="weui-cell__bd">
                <p v-if="cname == '请选择'"><span class="title">所在地区</span><span class="placeholder">{{cname}} {{dname}}</span></p>
                <p v-else><span class="title">所在地区</span>{{cname}} {{dname}}</p>
            </div>
            <div class="weui-cell__ft"></div>
        </a>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label go_top">详细地址</label></div>
            <!-- <div class="weui-cell__bd">
                <input class="weui-input" maxlength="50" type="text" placeholder="请输入" v-model='obj.teamdetailaddr' :value="obj.teamdetailaddr"/>
            </div> -->
            <div class="weui-cell__bd" id="myInput">
                    <textarea 
                        class="weui-textarea" 
                        placeholder="请输入" 
                        rows="2" 
                        maxlength="50" 
                        v-model="obj.teamdetailaddr"
                    ></textarea>
            </div>
        </div> 
    </div>
    <p style="text-align: center;margin-top: 10px;font-size: 13px;color: #BBBBBB;">创建企业后可统筹管理项目进展，并开启企业人才库功能</p>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import $ from 'zepto'
    import clientUi from 'gapi/clientUi.js'
    import widget from 'gapi/widget.js'
    import teamApi from 'clientApi/teamApi.js'
    import * as util from 'js/util.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                user_id:'',
                dmUserId:'',
                backWhereParam:'',
                noMoreClick:true,
                obj:{
                    teamname:'',
                    teamid:'',
                    industry:'请选择',
                    parentID:'',
                    childID:'',
                    parentText:'',
                    childText:'',
                    teamdetailname: '',
                    linkmanname: '',
                    linkmannumber: '',
                    teamdetailaddr: ''
                },
                industryData:[],
                pname:'',
                cname:'',
                dname:'',
                pid:'',
                cid:'',
                did:''
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                
                cache.deleteCache('addressArrData')
                
                if(this.cname == '' && this.dname == ''){
                    this.cname = '请选择'
                }
                commonPage.getWebViewID((data)=>{
                    cache.setMemoryCache('webViewID',{'webViewID':data})
                })
                window.loadData = (data) => {
                    if(data.type){
                        this.obj.industry = data.value.ctext
                        this.obj.parentText = data.value.ptext
                        this.obj.childText = data.value.ctext
                        this.obj.parentID = data.key.industrypid
                        this.obj.childID = data.key.industrycid
                    }else{
                        this.pid = data.key.province_id
                        this.cid = data.key.city_id
                        this.did = data.key.district_id
                        this.pname = data.value.province_name
                        this.cname = data.value.city_name
                        this.dname = data.value.district_name
                        cache.setCache('addressArrData', data, 86400*30) 
                    }
                }

            },
            /*行业选择*/
            industrySelect (industryName){
                let encodeUrl = encodeURIComponent(`industryName=${industryName}`)
                location.href = `dmsaas://SelectIndustry?urdData=${encodeUrl}`
            },
            /*创建团队*/
            teamcreatsubmit () {
                if(!this.obj.teamdetailname){
                    widget.toast('必须输入企业全称')
                    return
                }
                if(!this.obj.teamname){
                    widget.toast('必须输入企业简称')
                    return
                }
                if(!this.obj.linkmanname){
                    widget.toast('必须输入联系人')
                    return
                }
                if(!this.obj.linkmannumber){
                    widget.toast('必须输入手机号码')
                    return
                }
                if(!(/^\d{11}$/.test(this.obj.linkmannumber))){
                    widget.toast('手机号格式错误')
                    return                    
                }
                if(this.obj.industry == '请选择' || this.obj.industry == ''){
                    widget.toast('请选择行业')
                    return
                }
                if(this.cname == '请选择' || this.dname == '' || this.pname == ''){
                    widget.toast('请选择地区')
                    return
                }
                if(!this.obj.teamdetailaddr){
                    widget.toast('必须输入详细地址')
                    return
                }
                let that = this;
                let data = {
                    team_id:0,
                    project_id:0,
                    team_name:this.obj.teamname,
                    industry_1st_id:this.obj.parentID,
                    industry_1st_name:this.obj.parentText,
                    industry_2st_id:this.obj.childID,
                    industry_2st_name:this.obj.childText,
                    addr_province_id:this.pid,
                    addr_province_name:this.pname,
                    addr_city_id:this.cid,
                    addr_city_name:this.cname,
                    addr_district_id:this.did,
                    addr_district_name:this.dname,
                    name:this.obj.teamdetailname,
                    contact_person:this.obj.linkmanname,
                    mobile:this.obj.linkmannumber,
                    address:this.obj.teamdetailaddr
                }
                console.log(JSON.stringify(data))
                if(this.noMoreClick){
                    this.noMoreClick = false
                    util.ajax({
                        url:'/team/create',
                        type:'POST',
                        data:data,
                        timeout:10000,
                        success:(obj) => {
                            console.log(obj)
                            if(obj&&obj.errno == 0){
                                widget.toast('创建成功')  
                                // teamApi.updateTeam(); 
                                teamApi.updateTeam(obj.team_id);   
                                // let encodeStr = encodeURIComponent(`teamId=${obj.team_id}&dmUserId=${this.dmUserId}&backWhereParam=${this.backWhereParam}&team_name=${this.obj.teamname}&user_id=${this.user_id}`)
                                // location.href = `dmsaas://MemberManage?urdData=${encodeStr}`
                                cache.getMemoryCache('jcIndexWebViewID',function(data){
                                    if(data){
                                        commonPage.goBack(1, {}, data.webViewID)
                                    }
                                })
                            }else{
                                this.noMoreClick = true
                                widget.toast(obj.errmsg)
                            }
                            
                        },
                        error: (xhr, status) => {
                            this.noMoreClick = true
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            this.noMoreClick = true
                            widget.toast('网络连接失败，请检查网络')
                        }
                    })
                }
                
            }
        },
        created(){
            this.backWhereParam = util.getUrddataString('backWhereParam')
            this.dmUserId = util.getUrddataString('dmUserId')
            this.user_id = util.getUrddataString('user_id')
            this.init();

            /*设置Native的头部*/
            commonPage.updateTitleBar({
                rightDisplay: true,            // 是否显示右按钮
                rightText: '创建',            // 右按钮文案
                rightAction: 'finished'    // 点击右按钮的文案（即回调函数名称）
            })
            window.finished = () => {
                this.teamcreatsubmit()
            } 
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style>
    body{
        background: #f2f2f2;
    }
    a.weui-cell .weui-cell__ft{font-size: 17px;}
    a.weui-btn{margin: 20px 0;}
    div.weui-cell__ft{text-align: left;color: #333}
    .x-style{float: left;height: 44px;line-height: 44px;width: 33%;position: relative;}
    .weui-cell_select .weui-cell__bd:after{border-style:none;}
    .weui-cell_select .weui-cell__bd .x-style:after{
        content: " ";
        display: inline-block;
        height: 6px;
        width: 6px;
        border-width: 0px 2px 2px 0px;
        border-color: #C8C8CD;
        border-style: solid;
        -webkit-transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
        transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
        position: relative;
        top: -2px;
        position: absolute;
        top: 50%;
        right: 12px;
        margin-top: -3px;
    }
    .too_much {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .go_top {
        position: relative;
        top: -12px;
    }
</style>



